在 shinyapp 中显示来自 .csv 的 textInput 的反应值
show a reactive value for textInput from a .csv in a shinyapp
我正在开发一个 shinyapp,用于以交互方式生成 table,其中包含文件名和每个文件的一些新文本或评论。
目前它按照以下步骤工作:
1. 选择文件夹
2. 使用文件名创建 .csv (if none)
.csv 的名称遵循模式 "subfolderName".csv
3. 使用 selectInput 选项选择文件名
4. 将您对每一行(文件)的评论添加到 .csv 文件中
第二列名为 "comments"
一些我无法做到的期望行为:
- 在文本字段 (commentwritten) 中显示当前所选文件名(保存在 .csv 中)的先前评论(如果有)。我也尝试了 updateTextInput 但没有用。
#save as app.R
library(shiny)
library(shinyFiles)
library(shinyBS)
library(dplyr)
ui <- fluidPage(sidebarLayout(
sidebarPanel(
shinyDirButton("dir", "1. Choose directory", "Upload")
,br(),br()
, bsButton("submit","2. Create or reset .csv", style="warning")
,br(),br()
, uiOutput("fileinput")
, uiOutput("uitextinput") # this is not showing existing comments
, bsButton("addname","4. Add comment", style="warning")
),
mainPanel(
h4("Selected folder"),
verbatimTextOutput("dirtext"), br(),
h4("Files in that dir"),
verbatimTextOutput("files"),
textOutput("result")
)
))
path1<-"~"
server <- function(input, output, session) {
output$result<-renderText({
t<- reac$message
})
shinyDirChoose(input, 'dir', roots = c(home = path1) )
reacdir <- reactive(input$dir)
output$dirtext <- renderPrint(c(path(),current() ) )
path <- reactive({
home <- normalizePath(path1)
file.path(home, paste(unlist(reacdir()$path[-1]), collapse = .Platform$file.sep))
})
current<-reactive({
a<-sub('.*\/', '', path() )
b<-paste("current subdir:",a)
})
reac <- reactiveValues(comment=NULL, df2=NULL)
#not WORKING
output$uitextinput<-renderUI(
textInput("commentwritten","Comment for file", reac$comment)
)
# GET current .csv file with comments and load the comment to reac$comment , Not working
observe({
validate(
need(try(any(class(reactiveFileReader(1000, session, paste0(path(),"/",sub('.*\/', '', path() ),".csv") ,
read.csv()))=="data.frame")==TRUE), "Wait 801")
)
reac$df2<- reactiveFileReader(1000, session, paste0(path(),"/",sub('.*\/', '', path() ),".csv") , read.csv, stringsAsFactors=FALSE)
reac$comment<-reac$df2$comment[match(input$file, reac$df2$name)]
})
filedata<-reactive({
filenames<- list.files(path())
filedata<-data.frame(name=filenames)
})
# button 2. creating the csv file
observeEvent(input$submit,{
write.csv(filedata(), file =paste0(path(),"/",sub('.*\/', '', path() ),".csv") , row.names = FALSE )
})
#step 3
output$fileinput<-renderUI(
div(style="width: 100%; margin: 0 0;",
selectInput("file", "3. Select filename to add details", filedata()$name)
)
)
#step 4, adding a comment to the .csv
observeEvent(input$addname , {
if(file.exists(paste0(path(),"/",sub('.*\/', '', path() ),".csv")) ){
dfnameadded <- reactive({
originaldf<-reactiveFileReader(1000, session, paste0(path(),"/",sub('.*\/', '', path() ),".csv") , read.csv, stringsAsFactors=FALSE)
filetoadd <- data.frame(name=input$file , stringsAsFactors = FALSE)
filetoadd$comment<-input$commentwritten
l<-list(originaldf(),filetoadd)#
dfadded<- Reduce(bind_rows, l)
dfadded<-dfadded %>%
group_by(name) %>%
summarise(comment = paste0(na.omit(comment[length(comment)]), collapse = "; ") )
dfadded
})
tryCatch(write.csv(dfnameadded(), file =paste0(path(),"/",sub('.*\/', '', path() ),".csv") , row.names = FALSE ),
error = function(e) {print(paste("create .csv first")) ; "create .csv first" } )
reac$message<-"comment added"
}
else
{
reac$message<-"create .csv first"
}
})
output$files <- renderPrint(list.files(path()))
} # end server
shinyApp(ui, server)
试试这个:
reac <- reactiveValues(comment <- NULL, df <- NULL)
textInput("namewritten","Comment for photo",placeholder= if(is.null(reac$comment)){"No Comments"}else{reac$comment})
我也会将 df 设为 reactiveValue。您正在使用 observe 基本上调用反应函数,但 observe 不会 return 任何值,因此 reac$comment 不会 returned.
我想到了这个解决方案
正在替换
#not WORKING
# output$uitextinput<-renderUI(
# textInput("commentwritten","Comment for file", reac$comment)
# )
与:
observeEvent(input$file , {
if(file.exists(paste0(path(),"/",sub('.*\/', '', path() ),".csv")) ){
originaldf<-reactiveFileReader(1000, session, paste0(path(),"/",sub('.*\/', '', path() ),".csv"), read.csv, stringsAsFactors=FALSE)
reac$df<-originaldf()
output$uitextinput<-renderUI({
textInput("commentwritten","Comment for file",
reac$df$comment[match(input$file, reac$df$name)]
)
})
}
else{"index file does not exist, create with button 2."}
})
我正在开发一个 shinyapp,用于以交互方式生成 table,其中包含文件名和每个文件的一些新文本或评论。
目前它按照以下步骤工作:
1. 选择文件夹
2. 使用文件名创建 .csv (if none)
.csv 的名称遵循模式 "subfolderName".csv
3. 使用 selectInput 选项选择文件名
4. 将您对每一行(文件)的评论添加到 .csv 文件中
第二列名为 "comments"
一些我无法做到的期望行为:
- 在文本字段 (commentwritten) 中显示当前所选文件名(保存在 .csv 中)的先前评论(如果有)。我也尝试了 updateTextInput 但没有用。
#save as app.R
library(shiny)
library(shinyFiles)
library(shinyBS)
library(dplyr)
ui <- fluidPage(sidebarLayout(
sidebarPanel(
shinyDirButton("dir", "1. Choose directory", "Upload")
,br(),br()
, bsButton("submit","2. Create or reset .csv", style="warning")
,br(),br()
, uiOutput("fileinput")
, uiOutput("uitextinput") # this is not showing existing comments
, bsButton("addname","4. Add comment", style="warning")
),
mainPanel(
h4("Selected folder"),
verbatimTextOutput("dirtext"), br(),
h4("Files in that dir"),
verbatimTextOutput("files"),
textOutput("result")
)
))
path1<-"~"
server <- function(input, output, session) {
output$result<-renderText({
t<- reac$message
})
shinyDirChoose(input, 'dir', roots = c(home = path1) )
reacdir <- reactive(input$dir)
output$dirtext <- renderPrint(c(path(),current() ) )
path <- reactive({
home <- normalizePath(path1)
file.path(home, paste(unlist(reacdir()$path[-1]), collapse = .Platform$file.sep))
})
current<-reactive({
a<-sub('.*\/', '', path() )
b<-paste("current subdir:",a)
})
reac <- reactiveValues(comment=NULL, df2=NULL)
#not WORKING
output$uitextinput<-renderUI(
textInput("commentwritten","Comment for file", reac$comment)
)
# GET current .csv file with comments and load the comment to reac$comment , Not working
observe({
validate(
need(try(any(class(reactiveFileReader(1000, session, paste0(path(),"/",sub('.*\/', '', path() ),".csv") ,
read.csv()))=="data.frame")==TRUE), "Wait 801")
)
reac$df2<- reactiveFileReader(1000, session, paste0(path(),"/",sub('.*\/', '', path() ),".csv") , read.csv, stringsAsFactors=FALSE)
reac$comment<-reac$df2$comment[match(input$file, reac$df2$name)]
})
filedata<-reactive({
filenames<- list.files(path())
filedata<-data.frame(name=filenames)
})
# button 2. creating the csv file
observeEvent(input$submit,{
write.csv(filedata(), file =paste0(path(),"/",sub('.*\/', '', path() ),".csv") , row.names = FALSE )
})
#step 3
output$fileinput<-renderUI(
div(style="width: 100%; margin: 0 0;",
selectInput("file", "3. Select filename to add details", filedata()$name)
)
)
#step 4, adding a comment to the .csv
observeEvent(input$addname , {
if(file.exists(paste0(path(),"/",sub('.*\/', '', path() ),".csv")) ){
dfnameadded <- reactive({
originaldf<-reactiveFileReader(1000, session, paste0(path(),"/",sub('.*\/', '', path() ),".csv") , read.csv, stringsAsFactors=FALSE)
filetoadd <- data.frame(name=input$file , stringsAsFactors = FALSE)
filetoadd$comment<-input$commentwritten
l<-list(originaldf(),filetoadd)#
dfadded<- Reduce(bind_rows, l)
dfadded<-dfadded %>%
group_by(name) %>%
summarise(comment = paste0(na.omit(comment[length(comment)]), collapse = "; ") )
dfadded
})
tryCatch(write.csv(dfnameadded(), file =paste0(path(),"/",sub('.*\/', '', path() ),".csv") , row.names = FALSE ),
error = function(e) {print(paste("create .csv first")) ; "create .csv first" } )
reac$message<-"comment added"
}
else
{
reac$message<-"create .csv first"
}
})
output$files <- renderPrint(list.files(path()))
} # end server
shinyApp(ui, server)
试试这个:
reac <- reactiveValues(comment <- NULL, df <- NULL)
textInput("namewritten","Comment for photo",placeholder= if(is.null(reac$comment)){"No Comments"}else{reac$comment})
我也会将 df 设为 reactiveValue。您正在使用 observe 基本上调用反应函数,但 observe 不会 return 任何值,因此 reac$comment 不会 returned.
我想到了这个解决方案 正在替换
#not WORKING
# output$uitextinput<-renderUI(
# textInput("commentwritten","Comment for file", reac$comment)
# )
与:
observeEvent(input$file , {
if(file.exists(paste0(path(),"/",sub('.*\/', '', path() ),".csv")) ){
originaldf<-reactiveFileReader(1000, session, paste0(path(),"/",sub('.*\/', '', path() ),".csv"), read.csv, stringsAsFactors=FALSE)
reac$df<-originaldf()
output$uitextinput<-renderUI({
textInput("commentwritten","Comment for file",
reac$df$comment[match(input$file, reac$df$name)]
)
})
}
else{"index file does not exist, create with button 2."}
})