Trouble with the knit function in R Markdown and difficulty saving .rmd files

Hello,

I have been having some consistent problems with R markdown recently, starting from being unable to save R files as R documents to now being unable to knit documents.

When attempting to save the R markdown file it opens to the save box but the save as options only show the "All files" option, and then when I do click save it saves it as a plain notebook or just as a 'file'. This makes me lose all my chunk formatting and I have to open a new file and copy the text over to get r markdown formatting back.

I am currently trying to knit a document and it gets put to 84% done before I get a series of errors.
And previously when I did manage to knit a document it would not contain any of the R output and only had the chunks, even then the formatting of the chunks was occasionally messed up.

Error in file(file, ifelse(append, "a", "w")) : 
  cannot open the connection
Calls: <Anonymous> ... <Anonymous> -> png -> .handleSimpleError -> h -> cat -> file
In addition: Warning messages:
1: In dir.create(dirname(name), recursive = TRUE) :
  cannot create dir 'STWT07.1', reason 'No such file or directory'
2: In png(..., res = dpi, units = "in") :
  unable to open file 'STWT07.1/figure-docx/unnamed-chunk-7-1.png' for writing
3: In png(..., res = dpi, units = "in") : opening device failed
4: In file(file, ifelse(append, "a", "w")) :
  cannot open file 'STWT07.1.knit.md': No such file or directory
Execution halted

All the chunks run fine when I do them in RStudio.

Thus far I have tried:

  1. Giving RStudio permission to run as an administrative program
  2. Giving R, Rsession, and Rstudio.exe allowed apps permissions and controlled access permissions.
  3. Uninstalling and re-installing, while deleting old versions of the library so I only have the most updated version

If anyone has any further advice I would be very grateful.

Can you share a reproducible example of this error with the png device in an Rmd file ?

This would help see why this happens. Thanks!

Do you mean I should try to knit a different markdown file?

When I try to knit a different document it does not even get as far as the PNG device error instead it comes up with :

Error in abs_path(input) : 
  The file 'StvsCeleb-UtilitarianHedonic-Pretest2.rmd' does not exist.
Calls: <Anonymous> -> setwd -> dirname -> abs_path
In addition: Warning messages:
1: In file.create(to[okay]) :
  cannot create file 'StvsCeleb-UtilitarianHedonic-Pretest2.rmd', reason 'No such file or directory'
2: In normalizePath(path.expand(path), winslash, mustWork) :
  path[1]="StvsCeleb-UtilitarianHedonic-Pretest2.rmd": The system cannot find the file specified
Execution halted

And a third file that I attempted to knit showed this error:


processing file: STvsCeleb-Review_Pretest_clean.Rmd
  |....                                                                  |   6%
  ordinary text without R code

  |........                                                              |  11%
label: setup (with options) 
List of 1
 $ include: logi FALSE

  |............                                                          |  17%
  ordinary text without R code

  |................                                                      |  22%
label: unnamed-chunk-1
  |...................                                                   |  28%
  ordinary text without R code

  |.......................                                               |  33%
label: unnamed-chunk-2
  |...........................                                           |  39%
  ordinary text without R code

  |...............................                                       |  44%
label: unnamed-chunk-3
  |...................................                                   |  50%
  ordinary text without R code

  |.......................................                               |  56%
label: unnamed-chunk-4
Error in file(file, ifelse(append, "a", "w")) : 
  cannot open the connection
Calls: <Anonymous> ... signal_abort -> signalCondition -> <Anonymous> -> cat -> file
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
  cannot open file 'STvsCeleb-Review_Pretest_clean.knit.md': No such file or directory

Execution halted

It seems it is something about this particular file that sets off the PNG error

Show in New Window
This is lavaan 0.6-8
lavaan is FREE software! Please report any bugs.
Registered S3 method overwritten by 'data.table':
  method           from
  print.data.table     
Registered S3 method overwritten by 'htmlwidgets':
  method           from         
  print.htmlwidget tools:rstudio
Registered S3 methods overwritten by 'broom':
  method            from  
  tidy.glht         jtools
  tidy.summary.glht jtools
Registered S3 methods overwritten by 'dbplyr':
  method         from
  print.tbl_lazy     
  print.tbl_sql      
Warning in (function (kind = NULL, normal.kind = NULL, sample.kind = NULL)  :
  non-uniform 'Rounding' sampler used
-- Attaching packages ----------------------------------------------------------------------- tidyverse 1.3.1 --
v ggplot2 3.3.3     v purrr   0.3.4
v tibble  3.1.2     v dplyr   1.0.6
v tidyr   1.1.3     v stringr 1.4.0
v readr   1.4.0     v forcats 0.5.1
Warning in (function (kind = NULL, normal.kind = NULL, sample.kind = NULL)  :
  non-uniform 'Rounding' sampler used
-- Conflicts -------------------------------------------------------------------------- tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()

-- Column specification ----------------------------------------------------------------------------------------
cols(
  .default = col_double(),
  StartDate = col_character(),
  EndDate = col_character(),
  IPAddress = col_character(),
  RecordedDate = col_character(),
  ResponseId = col_character(),
  RecipientLastName = col_logical(),
  RecipientFirstName = col_logical(),
  RecipientEmail = col_logical(),
  ExternalReference = col_logical(),
  DistributionChannel = col_character(),
  UserLanguage = col_character(),
  Q1.3_Browser = col_character(),
  Q1.3_Version = col_character(),
  `Q1.3_Operating System` = col_character(),
  Q1.3_Resolution = col_character(),
  Q2.2_3_TEXT = col_logical(),
  PROLIFIC_PID = col_character()
)
i Use `spec()` for the full column specifications.

Show in New Window
            Df Sum Sq Mean Sq F value Pr(>F)
Group        1   1.14   1.139   1.301  0.257
Residuals   80  70.08   0.876               
3 observations deleted due to missingness
Show in New Window
            Df Sum Sq Mean Sq F value Pr(>F)
Group        1   2.58   2.575    1.87  0.175
Residuals   80 110.20   1.378               
3 observations deleted due to missingness
            Df Sum Sq Mean Sq F value Pr(>F)  
Group        1   5.06   5.060   4.029 0.0481 *
Residuals   80 100.46   1.256                 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
3 observations deleted due to missingness
            Df Sum Sq Mean Sq F value Pr(>F)
Group        1   0.72  0.7215   0.483  0.489
Residuals   80 119.58  1.4948               
3 observations deleted due to missingness
            Df Sum Sq Mean Sq F value Pr(>F)
Group        1   0.64  0.6381    0.43  0.514
Residuals   80 118.84  1.4855               
3 observations deleted due to missingness
            Df Sum Sq Mean Sq F value Pr(>F)  
Group        1   8.25   8.250   5.587 0.0205 *
Residuals   80 118.14   1.477                 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
3 observations deleted due to missingness
            Df Sum Sq Mean Sq F value Pr(>F)
Group        1   0.02  0.0189   0.014  0.906
Residuals   80 106.86  1.3357               
3 observations deleted due to missingness
Show in New Window
A tibble:6 x 20
Rev1Cred
<dbl>
Rev1Match
<dbl>
Rev1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side
<dbl>
Rev3Pos
<dbl>
1	1	1.0	5	1	3	1	2.5	4	3	
2	2	2.0	3	4	2	2	2.0	3	4	
4	3	4.0	5	5	2	2	2.5	4	3	
3	3	3.5	4	4	2	1	3.0	4	4	
2	1	2.5	3	3	2	1	2.5	5	4	
3	4	4.0	3	4	5	5	4.5	2	5	
6 rows | 1-10 of 20 columns
A tibble:6 x 20
Rev1Cred
<dbl>
Rev1Match
<dbl>
Rev1Qual
<dbl>
Rev1Side
<dbl>
Rev1Pos
<dbl>
Rev3Cred
<dbl>
Rev3Match
<dbl>
Rev3Qual
<dbl>
Rev3Side
<dbl>
Rev3Pos
<dbl>
1	1	1.0	5	1	3	1	2.5	4	3	
2	2	2.0	3	4	2	2	2.0	3	4	
4	3	4.0	5	5	2	2	2.5	4	3	
3	3	3.5	4	4	2	1	3.0	4	4	
2	1	2.5	3	3	2	1	2.5	5	4	
3	4	4.0	3	4	5	5	4.5	2	5	
6 rows | 1-10 of 20 columns
Show in New Window
Warning: attributes are not identical across measure variables;
they will be dropped
Warning: attributes are not identical across measure variables;
they will be dropped
Warning: Removed 60 rows containing non-finite values (stat_boxplot).
Warning: attributes are not identical across measure variables;
they will be dropped
Warning: attributes are not identical across measure variables;
they will be dropped
Warning: Removed 60 rows containing non-finite values (stat_boxplot).
Warning: attributes are not identical across measure variables;
they will be dropped
Warning: attributes are not identical across measure variables;
they will be dropped
Warning: Removed 60 rows containing non-finite values (stat_boxplot).
Warning: attributes are not identical across measure variables;
they will be dropped
Warning: attributes are not identical across measure variables;
they will be dropped
Warning: Removed 60 rows containing non-finite values (stat_boxplot).
R Console
A tibble:6 x 3
value
<dbl>
Review.no
<chr>
Measure
<chr>
1	1	Credibility		
2	1	Credibility		
4	1	Credibility		
3	1	Credibility		
2	1	Credibility		
3	1	Credibility		
6 rows
tbl_df
6 x 3
A tibble:6 x 4
mean
<dbl>
sd
<dbl>
Review.no
<chr>
Measure
<chr>
3.097561	0.9376850	1	Credibility	
2.878049	1.1799795	1	Match-up	
4.146341	0.9043373	1	Positive	
3.195122	1.0084706	1	Quality	
3.621951	1.0498706	1	Sidedness	
3.109756	1.0541640	3	Credibility	
6 rows
tbl_df
6 x 4



Show in New Window
Description:df [5 x 3]
comparison
<chr>
t
<dbl>
p
<dbl>
Cred1v6	0.5639179	0.5735931		
Match1v6	0.4023581	0.6879513		
Qual1v6	0.0392624	0.9687296		
Side1v6	-1.3109783	0.1917235		
Pos1v6	-0.8705511	0.3852878		
5 rows
data.frame
5 x 3
Description:df [5 x 3]
comparison
<chr>
t
<dbl>
p
<dbl>
Cred3v4	-2.836457	0.0051676304		
Match3v4	-1.797160	0.0741730199		
Qual3v4	-3.363027	0.0009692438		
Side3v4	-1.471854	0.1430011789		
Pos3v4	-3.185327	0.0017547494		
5 rows
data.frame
5 x 3
Description:df [5 x 3]
comparison
<chr>
t
<dbl>
p
<dbl>
Cred3v4	-2.836457	0.0051676304		
Match3v4	-1.797160	0.0741730199		
Qual3v4	-3.363027	0.0009692438		
Side3v4	-1.471854	0.1430011789		
Pos3v4	-3.185327	0.0017547494		
5 rows


processing file: STWT07.1-Celeb-Sports-review-prestest.rmd
  |....                                                                  |   5%
  ordinary text without R code

  |.......                                                               |  11%
label: setup
  |...........                                                           |  16%
  ordinary text without R code

  |...............                                                       |  21%
label: unnamed-chunk-1 (with options) 
List of 1
 $ results: chr "hide"

  |..................                                                    |  26%
  ordinary text without R code

  |......................                                                |  32%
label: unnamed-chunk-2
  |..........................                                            |  37%
  ordinary text without R code

  |.............................                                         |  42%
label: unnamed-chunk-3
  |.................................                                     |  47%
  ordinary text without R code

  |.....................................                                 |  53%
label: unnamed-chunk-4
  |.........................................                             |  58%
  ordinary text without R code

  |............................................                          |  63%
label: unnamed-chunk-5
  |................................................                      |  68%
  ordinary text without R code

  |....................................................                  |  74%
label: unnamed-chunk-6
  |.......................................................               |  79%
  ordinary text without R code

  |...........................................................           |  84%
label: unnamed-chunk-7

Error in file(file, ifelse(append, "a", "w")) : 
  cannot open the connection
Calls: <Anonymous> ... <Anonymous> -> png -> .handleSimpleError -> h -> cat -> file
In addition: Warning messages:
1: In dir.create(dirname(name), recursive = TRUE) :
  cannot create dir 'STWT07.1-Celeb-Sports-review-prestest_files', reason 'No such file or directory'
2: In png(..., res = dpi, units = "in") :
  unable to open file 'STWT07.1-Celeb-Sports-review-prestest_files/figure-docx/unnamed-chunk-7-1.png' for writing
3: In png(..., res = dpi, units = "in") : opening device failed
4: In file(file, ifelse(append, "a", "w")) :
  cannot open file 'STWT07.1-Celeb-Sports-review-prestest.knit.md': No such file or directory
Execution halted

The files are rather long, but would you like the code to see how they differ? This is because I have not come across this error when I ran any of the chunks by themselves.

I mean that having an example I can run on my side that reproduce this would be helpful.

Currently the error only is not helpful - it just indicate that something seems not right with the PNG device at some point.

Can you try simplify your file to keep only what reproduce the error and share this shorter file ?
If it is possible to reproduce on shorter file obviously

Hi,

The problem is I am not sure where the error is coming up but I have discovered it is affecting other aspects of R's image production. When I tried to copy graph images directly from the R output I was unable to copy them or even save them as save-as files (I had the same problem as before as the only save option was 'all files). It seems the only way I could get the images out of R was to take a snip/screenshot of them.

I am not sure if any code I give you will be helpful as it seems to only affect when I try to export anything out of R. I tried to write some simpler CSV files to export and give you some simpler code but that also failed, R was unable to write the CSV files in a readable format.

It seems the main problem is anything to do with R exporting files regardless of file type.
The code I used to produce the graphs was:

ggplot(SCVar_frame2) +
 aes(x = Review.no, y = value, fill = Measure) +
 geom_boxplot() +
 scale_fill_hue() +
  coord_cartesian(ylim=c(2, 5))+
 labs(y = "Measure Scores", title = "Experience Product Reviews") +
 theme_classic()

ggplot(SCVar_frame) +
 aes(x = Review.no, fill = Measure, weight = mean) +
 geom_bar(position = "dodge") +
 scale_fill_brewer(palette = "Set1") +
  ylim(0,5)+
 labs(x = "Review Number", y = "Average Measure Score", title = "Review Evaluation") +
 theme_classic()

I wrote the code using the ESQUISSE ggplot2 builder so I did not think there was much wrong with that and it works within R.