有闪亮的反应性问题

发布于 2025-01-25 10:53:31 字数 1179 浏览 4 评论 0原文

我尝试显示与Selecinput标准匹配的行数,并有可能添加注释。 每次有人添加评论时,它都会添加一行,我想更新行数。 (实际上,我正在处理一个评论部分,我想在其中显示播放器中的所有注释并显示提交的所有新评论)

,以输出$ texte,当SelectInput更改时,DF是子集,因此反应性工作。当我有观察到的时,DF就会保存,但没有反应性。当我切换选定的播放器时,它永远不会考虑到DF的变化,就像他一次导入DF并且从未为显示器更新它。

感谢您的帮助

df<-data.frame(nom=c("name1","name2","name3","name"1),
              comment=c("comment 1","comment 2","comment 3","comment4"))

ui<-  sidebarLayout(
        sidebarPanel(
          selectInput(
            inputId="select_j",
            label="choose name",
            choices=c("name1","name2","name3")
          )
        ),
        mainPanel(
          fluidRow(textOutput("texte")),
          fluidRow(textInput(
            inputId = "comment",
            label="Make a comment")),
          actionButton("submit", "Submit"))
    
)

server<-function(input,output){
  df_filter=reactive({df %>% subset(nom %in% input$select_j)})
  
  observeEvent(input$submit,{
    new_line=c(input$select_j,input$comment)
    df[nrow(df) + 1,] <- new_line
    write.csv2(df,"save.csv")
  })
  
  output$texte <- renderText({ 
    input$submit
    nrow(df_filter())
  })

}

shinyApp(ui, server)

I try to display the number of lines that matchs selecinput criteria with a possibility to add comment.
Everytime someone add a comment, it s add a line, and i would like to update the number of line.
(Actually i am working on a comment section where I want to display all the comments from a player and display every new comment submitted)

For the output$texte, when the selectinput changes, the df is subset, so the reactive works. When i have an observeEvent, df is saved, but there is no reactivity. when I switch the selected players, it never take into account the change in df, it's like he imports df once and never update it for the display.

Thanks for your help

df<-data.frame(nom=c("name1","name2","name3","name"1),
              comment=c("comment 1","comment 2","comment 3","comment4"))

ui<-  sidebarLayout(
        sidebarPanel(
          selectInput(
            inputId="select_j",
            label="choose name",
            choices=c("name1","name2","name3")
          )
        ),
        mainPanel(
          fluidRow(textOutput("texte")),
          fluidRow(textInput(
            inputId = "comment",
            label="Make a comment")),
          actionButton("submit", "Submit"))
    
)

server<-function(input,output){
  df_filter=reactive({df %>% subset(nom %in% input$select_j)})
  
  observeEvent(input$submit,{
    new_line=c(input$select_j,input$comment)
    df[nrow(df) + 1,] <- new_line
    write.csv2(df,"save.csv")
  })
  
  output$texte <- renderText({ 
    input$submit
    nrow(df_filter())
  })

}

shinyApp(ui, server)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫林﹌晚霞¤ 2025-02-01 10:53:31

我找到了解决方案(也许不是唯一的解决方案)。

df必须设置为reactiveVal,然后在观察事件时进行更新

df<-data.frame(
    nom=c("name1","name2", "name3"),
    comment=c("comment 1", "comment 2", "comment 3")
)

ui<-sidebarLayout(
    sidebarPanel(
        selectInput(
            inputId="select_j",
            label="choose name",
            choices=c("name1","name2","name3")
        )
    ),
    mainPanel(
        fluidRow(textOutput("texte")),
        fluidRow(textInput(
            inputId="comment",
            label="Make a comment")
        ),
        actionButton("submit", "Submit"))
    )

    server<-function(input, output) {
        df_r=reactiveVal(df)
        df_filter=reactive({
            df_r() %>% subset(nom %in% input$select_j)
        })
  
        observeEvent(input$submit,{
            new_line=c(input$select_j, input$comment)
            new_df<-rbind(df_r(), new_line)
            df_r(new_df) #update the reactive value
            write.csv2(new_df, "save.csv")
        })
  
        output$texte <- renderText({ 
            input$submit
            nrow(df_filter())
        })
    }

    shinyApp(ui, server)

I have found the solution (maybe not the only one).

The df has to be set as a reactiveVal and then update when observe event

df<-data.frame(
    nom=c("name1","name2", "name3"),
    comment=c("comment 1", "comment 2", "comment 3")
)

ui<-sidebarLayout(
    sidebarPanel(
        selectInput(
            inputId="select_j",
            label="choose name",
            choices=c("name1","name2","name3")
        )
    ),
    mainPanel(
        fluidRow(textOutput("texte")),
        fluidRow(textInput(
            inputId="comment",
            label="Make a comment")
        ),
        actionButton("submit", "Submit"))
    )

    server<-function(input, output) {
        df_r=reactiveVal(df)
        df_filter=reactive({
            df_r() %>% subset(nom %in% input$select_j)
        })
  
        observeEvent(input$submit,{
            new_line=c(input$select_j, input$comment)
            new_df<-rbind(df_r(), new_line)
            df_r(new_df) #update the reactive value
            write.csv2(new_df, "save.csv")
        })
  
        output$texte <- renderText({ 
            input$submit
            nrow(df_filter())
        })
    }

    shinyApp(ui, server)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文