简体   繁体   English

从Shiny Server用新行(\\ n)写入文本文件

[英]Write text file with new line (\n) from Shiny Server

I have an app which generates a text file. 我有一个生成文本文件的应用程序。 I use \\n to put text on new lines. 我使用\\n将文本放在新行上。 If I run the app locally, the downloaded txt file has the correct output. 如果我在本地运行该应用程序,则下载的txt文件具有正确的输出。 However, if I run the app on Shiny Server, the text does not print on new lines ie \\n does not seem to work. 但是,如果我在Shiny Server上运行该应用程序,则该文本不会在新行上打印,即\\n似乎不起作用。 Here is an example app: 这是一个示例应用程序:

ui <- fluidPage(title = "",
                sidebarLayout(
                  sidebarPanel(
                    numericInput("num", "Number of lines to paste", 3),
                    actionButton("gen", "Generate text with new lines"),
                    downloadButton("downloadWeld", "Download")
                  ),
                  mainPanel(
                    verbatimTextOutput("vtxt")
                  ))

)

server <- function(input, output) {

  values <- reactiveValues()
  values$df <- c()

  observeEvent(input$gen,{
    values$df <- NULL    
    values$df <- paste0("E",input$num,"\n")

    for(i in 1:input$num)
      values$df <- paste0(values$df, "hello\n",i,"\nworld\n")
  })

  output$vtxt <- renderText({
    values$df
  })

  output$downloadWeld <- downloadHandler(
    filename <- function() {
      paste0(input$num,"fname.txt")
    },

    content <- function(file) {
      writeLines(values$df, file)
    },
    contentType = "text/csv"
  )

}    
shinyApp(ui = ui, server = server)

Here is my sessionInfo for local: 这是我本地的sessionInfo:

R version 3.3.1 (2016-06-21)

Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_2.2.1 shiny_1.0.2  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.7      assertthat_0.1   digest_0.6.10    mime_0.5        
 [5] grid_3.3.1       R6_2.1.2         plyr_1.8.4       xtable_1.8-2    
 [9] jsonlite_1.0     gtable_0.2.0     scales_0.4.1     lazyeval_0.2.0  
[13] labeling_0.3     tools_3.3.1      munsell_0.4.3    httpuv_1.3.3    
[17] colorspace_1.2-6 htmltools_0.3.5  tibble_1.2 

and server: 和服务器:

R version 3.3.3 (2017-03-06)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8       
 [4] LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] pool_0.1.0           data.table_1.9.6     sqldf_0.4-10         RSQLite_1.0.0       
 [5] gsubfn_0.6-6         proto_0.3-10         RMySQL_0.10.9        DBI_0.5-1           
 [9] dplyr_0.5.0          ggthemes_2.2.1       scales_0.4.1         plyr_1.8.4          
[13] shinydashboard_0.5.1 shinythemes_1.1.1    markdown_0.7.7       shiny_1.0.2         
[17] ggplot2_2.2.1       

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.8      magrittr_1.5     munsell_0.4.3    colorspace_1.3-2 xtable_1.8-0     R6_2.2.0        
 [7] tcltk_3.3.3      tools_3.3.3      grid_3.3.3       gtable_0.2.0     htmltools_0.3.5  lazyeval_0.2.0  
[13] assertthat_0.1   digest_0.6.11    tibble_1.2       mime_0.4         labeling_0.3     jsonlite_1.1    
[19] chron_2.3-47     httpuv_1.3.3 

Any ideas why I can't get the correct output on the server app? 为什么我无法在服务器应用程序上获得正确的输出?

I found another thread with the answer . 我找到了另一个答案 It it because on the Shiny Server the apps are running with Java and therefore a new line is saved by using: 这是因为在Shiny Server上,应用程序都使用Java运行,因此使用以下命令保存了新行:

\\r\\n

I guess when running locally there is no need for Java conversion. 我想在本地运行时不需要进行Java转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM